package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.ZhouNianQingRankData;
import com.yanqu.road.dataHelper.action.zhounianqing.*;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.dataHelper.utils.TextData;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ZhouNianQingRank extends actionBase {
    public void go(List<Long> serverList) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        String dirPath = System.getProperty("user.dir") + File.separator;

        List<ZhouNianQingRankData> allRankDataList = new ArrayList<>();


        int size = 100;
        //6_亲密度冲榜---------------
        List<Integer> intimacyTypeList = new ArrayList<>();
        intimacyTypeList.add(18);

        List<Future> futureList = new ArrayList<>();
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                //美女冲榜
                BeautyDataETLCaller beautyDataETLCaller = new BeautyDataETLCaller(intimacyTypeList, 6, serverInfo, size);
                Future future = executorService.submit(beautyDataETLCaller);
                futureList.add(future);
            }
        }
        try {
            for (Future future : futureList) {
                List<ZhouNianQingRankData> rankData = (List<ZhouNianQingRankData>)future.get();
                allRankDataList.addAll(rankData);
            }
        }catch (Exception e){
            e.printStackTrace(System.out);
        }
        sortAndWriteFile(allRankDataList,"6_亲密度冲榜");
        sortAndInsert(allRankDataList);

        allRankDataList = new ArrayList<>();
        //2_子嗣冲榜---------------
        List<Integer> childEarnSpeedTypeList = new ArrayList<>();
        childEarnSpeedTypeList.add(42);

        futureList = new ArrayList<>();
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                //子嗣冲榜
                ChildDataETLCaller caller = new ChildDataETLCaller(childEarnSpeedTypeList, 2, serverInfo, size);
                Future future = executorService.submit(caller);
                futureList.add(future);
            }
        }
        try {
            for (Future future : futureList) {
                List<ZhouNianQingRankData> rankData = (List<ZhouNianQingRankData>)future.get();
                allRankDataList.addAll(rankData);
            }
        }catch (Exception e){
            e.printStackTrace(System.out);
        }
        sortAndWriteFile(allRankDataList, "2_子嗣冲榜");
        sortAndInsert(allRankDataList);

        allRankDataList = new ArrayList<>();
        //4_门客冲榜
        List<Integer> patronsEarnSpeedTypeList = new ArrayList<>();
        patronsEarnSpeedTypeList.add(16);

        futureList = new ArrayList<>();
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                //门客冲榜
                PatronsDataETLCaller caller = new PatronsDataETLCaller(patronsEarnSpeedTypeList, 4, serverInfo, size);
                Future future = executorService.submit(caller);
                futureList.add(future);
            }
        }
        try {
            for (Future future : futureList) {
                List<ZhouNianQingRankData> rankData = (List<ZhouNianQingRankData>)future.get();
                allRankDataList.addAll(rankData);
            }
        }catch (Exception e){
            e.printStackTrace(System.out);
        }
        sortAndWriteFile(allRankDataList, "4_门客冲榜");
        sortAndInsert(allRankDataList);

        allRankDataList = new ArrayList<>();
        //3_宴会冲榜
        List<Integer> banquetTypeList = new ArrayList<>();
        banquetTypeList.add(53);

        futureList = new ArrayList<>();
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                //宴会冲榜
                BanquetDataETLCaller caller = new BanquetDataETLCaller(banquetTypeList, 3, serverInfo, size);
                Future future = executorService.submit(caller);
                futureList.add(future);
            }
        }
        try {
            for (Future future : futureList) {
                List<ZhouNianQingRankData> rankData = (List<ZhouNianQingRankData>)future.get();
                allRankDataList.addAll(rankData);
            }
        }catch (Exception e){
            e.printStackTrace(System.out);
        }
        sortAndWriteFile(allRankDataList, "3_宴会冲榜");
        sortAndInsert(allRankDataList);

        allRankDataList = new ArrayList<>();
        //5_赚速冲榜
        List<Integer> earnsTypeList = new ArrayList<>();
        earnsTypeList.add(14);

        futureList = new ArrayList<>();
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                //赚速冲榜
                UserEarnsDataETLCaller caller = new UserEarnsDataETLCaller(earnsTypeList, 5, serverInfo, size);
                Future future = executorService.submit(caller);
                futureList.add(future);
            }
        }
        try {
            for (Future future : futureList) {
                List<ZhouNianQingRankData> rankData = (List<ZhouNianQingRankData>)future.get();
                allRankDataList.addAll(rankData);
            }
        }catch (Exception e){
            e.printStackTrace(System.out);
        }
        sortAndWriteFile(allRankDataList, "5_赚速冲榜");
        sortAndInsert(allRankDataList);

        allRankDataList = new ArrayList<>();
        //1_财神
        futureList = new ArrayList<>();
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                //财神
                MammonDataETLCaller caller = new MammonDataETLCaller(new ArrayList<>(), 1, serverInfo, size);
                Future future = executorService.submit(caller);
                futureList.add(future);
            }
        }
        try {
            for (Future future : futureList) {
                List<ZhouNianQingRankData> rankData = (List<ZhouNianQingRankData>)future.get();
                allRankDataList.addAll(rankData);
            }
        }catch (Exception e){
            e.printStackTrace(System.out);
        }
        sortAndWriteFileForMammon(allRankDataList, "1_财神");
        sortAndInsertForMammon(allRankDataList);
    }

    public void sortAndWriteFile(List<ZhouNianQingRankData> allRankDataList, String fileName) {
        List<ZhouNianQingRankData> resultList;
        List<String> sqlList;
        int i;
        allRankDataList.sort(Comparator.comparing(o -> new BigInteger(o.getValue()).multiply(BigInteger.valueOf(-1))));
        resultList = allRankDataList.subList(0, 100 > allRankDataList.size() ? allRankDataList.size() : 100);
        //最后100名
        //结果写文件
        sqlList = new ArrayList<>();
        i = 1;
        for (ZhouNianQingRankData rankData : resultList) {
            sqlList.add(rankData.toInsertSql(i++) + ";");
        }
        try {
            TextData.outTextData(fileName, sqlList);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public void sortAndWriteFileForMammon(List<ZhouNianQingRankData> allRankDataList, String fileName) {
        List<ZhouNianQingRankData> resultList;
        List<String> sqlList;
        int i;
        MammonRankDataSort sort = new MammonRankDataSort();
        allRankDataList.sort(sort);
        resultList = allRankDataList.subList(0, 100 > allRankDataList.size() ? allRankDataList.size() : 100);
        //最后100名
        //结果写文件
        sqlList = new ArrayList<>();
        i = 1;
        for (ZhouNianQingRankData rankData : resultList) {
            sqlList.add(rankData.toInsertSql(i++) + ";");
        }
        try {
            TextData.outTextData(fileName, sqlList);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public void sortAndInsert(List<ZhouNianQingRankData> allRankDataList) {
        List<ZhouNianQingRankData> resultList;
        allRankDataList.sort(Comparator.comparing(o -> new BigInteger(o.getValue()).multiply(BigInteger.valueOf(-1))));
        resultList = allRankDataList.subList(0, 100 > allRankDataList.size() ? allRankDataList.size() : 100);
        //100名
        try {
            new ServerMgr().addRankData2(resultList);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public void sortAndInsertForMammon(List<ZhouNianQingRankData> allRankDataList) {
        List<ZhouNianQingRankData> resultList;
        MammonRankDataSort sort = new MammonRankDataSort();
        allRankDataList.sort(sort);
        resultList = allRankDataList.subList(0, 100 > allRankDataList.size() ? allRankDataList.size() : 100);
        //100名
        try {
            new ServerMgr().addRankData2(resultList);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}
